package com.yhao.stock.common.stock;

import cn.hutool.db.Entity;
import com.yhao.stock.common.utils.JdbcUtil;
import com.yhao.stock.entity.DayInfo;
import com.yhao.stock.entity.HisData2023;
import com.yhao.stock.entity.StockInfo;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : yhao
 * @Date : 2023/12/6 - 7:38
 * @Desc : TODO
 */
@Slf4j
public class StockBaseUtil {

    /**
     * @return
     * @desc 获取股票信息
     */
    public static List<DayInfo> getStock() {
        String sql = "select DISTINCT d_code,d_name from day_info where d_code not like '8%' and d_code not like '68%' and d_code not like '4%' and d_name not LIKE '%ST%'";
        List<DayInfo> resultList = null;
        try {
            resultList = JdbcUtil.getDb().query(sql, DayInfo.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultList;
    }

    public static List<DayInfo> getStockLimit10() {
        String sql = "select DISTINCT d_code,d_name from day_info where d_code not like '8%' and d_code not like '68%' and d_code not like '4%' and d_name not LIKE '%ST%' limit 10";
        List<DayInfo> resultList = null;
        try {
            resultList = JdbcUtil.getDb().query(sql, DayInfo.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultList;
    }

    public static List<DayInfo> getStockByCode(String code) {
        String sql = "select DISTINCT d_code,d_name from day_info where d_code = '" + code + "'";
        List<DayInfo> resultList = null;
        try {
            resultList = JdbcUtil.getDb().query(sql, DayInfo.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultList;
    }

    public static Double getPriceByDate(String code, String date) {
        String sql = "select now_price from day_info where d_code = '" + code + "' and d_date = '" + date + "'";
        Entity entity = null;
        try {
            entity = JdbcUtil.getDb().queryOne(sql, null);
        } catch (SQLException throwables) {
            log.info("code=[" + code + "],name=[" + date + "]-----未查到该信息");
        }
        if (entity != null) {
            return Double.parseDouble(entity.getStr("now_price"));
        }
        return Double.parseDouble("-1");
    }

    /**
     * @param code
     * @return
     * @desc 根据code 获取股票信息
     */
    public static List<DayInfo> getDayInfoByCode(String code) {
        List<DayInfo> dayInfoList = null;
        String sql = "select * from day_info where d_code = '" + code + "' ORDER BY d_date asc";
        try {
            dayInfoList = JdbcUtil.getDb().query(sql, DayInfo.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return dayInfoList;
    }

    /**
     * @param dataList
     * @return
     * @desc 计算连续下跌
     */
    public static List<Integer> sdCount(Integer tickCount, List<Double> dataList) {
        double beforeValue = 0;
        List<Integer> resultList = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < dataList.size(); i++) {
            Double currentValue = dataList.get(i);
            // 前值大于当前值
            if (beforeValue >= currentValue) {
                count++;
            } else {
                if (count >= tickCount) {
                    resultList.add(count);
                }
                count = 0;
            }
            beforeValue = currentValue;
        }
        return resultList;
    }

    public static Integer dayInfoSdCountNew1214(List<DayInfo> dataList) {

        BigDecimal lastPrice = dataList.get(0).getNowPrice();
        Integer result = 0;
        BigDecimal beforeAvgPrice = new BigDecimal(-1);
        BigDecimal beforeSubPrice = new BigDecimal(-1);
        BigDecimal avgPrice = null;
        for (int i = 3; i < dataList.size(); i++) {
            List<DayInfo> dayInfoList = dataList.subList(1, i);
            avgPrice = getAvgPrice(dayInfoList);

            List<DayInfo> dayInfoListSub = null;
            if (i == dataList.size() - 1) {
                dayInfoListSub = dataList.subList(i - 2, i + 1);
            } else if (i == dataList.size()) {
                dayInfoListSub = dataList.subList(i - 2, i);
            } else {
                dayInfoListSub = dataList.subList(i - 2, i + 2);
            }
            BigDecimal avgPriceSub = getAvgPrice(dayInfoListSub);
            System.out.println("lastPrice==>>" + lastPrice);
            System.out.println("beforeAvgPrice==>>" + beforeAvgPrice + "    avgPrice==>>" + avgPrice);
            System.out.println("beforeSubPrice==>>" + beforeSubPrice + "    avgPriceSub==>>" + avgPriceSub);
            if (lastPrice.compareTo(avgPrice) <= 0 && beforeAvgPrice.compareTo(avgPrice) <= 0 && lastPrice.compareTo(avgPriceSub) < 0 && beforeSubPrice.compareTo(avgPriceSub) < 0) {
                result = result + 1;
                beforeAvgPrice = avgPrice;
                beforeSubPrice = avgPriceSub;
            } else {
                return result;
            }

            System.out.println("--------------------------------------------");
        }

        return result;
    }

    public static BigDecimal getAvgPrice(List<DayInfo> dataList) {
        BigDecimal lastAvgPrice = new BigDecimal(0);
        for (DayInfo dayInfo : dataList) {
            lastAvgPrice = lastAvgPrice.add(dayInfo.getNowPrice());
        }
        return lastAvgPrice.divide(new BigDecimal(dataList.size()), 2, RoundingMode.HALF_UP);
    }

    /**
     * @param dataList
     * @return
     * @desc 求股票连续下跌数量
     */
    public static Integer declineCount(List<DayInfo> dataList) {

        BigDecimal currentPrice = dataList.get(0).getNowPrice();
        Integer result = 0;
        for (int i = 1; i < dataList.size(); i++) {
            BigDecimal nowPrice = dataList.get(i).getNowPrice();// 当前价格
            if (currentPrice.compareTo(nowPrice) <= 0) {
                result = result + 1;
                currentPrice = nowPrice;
            } else {
                return result;
            }
        }
        System.out.println("------------------------------------------------------------");
        return result;
    }

    /**
     * @param tickCount
     * @param dataList
     * @return
     * @desc 计算Dayinfo表连续下跌数量
     */
    public static Integer dayInfoSdCount(Integer tickCount, List<DayInfo> dataList) {
        BigDecimal beforeValue = new BigDecimal(0);
        List<Integer> resultList = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < dataList.size(); i++) {
            BigDecimal nowPrice = dataList.get(i).getNowPrice();
            // 前值大于当前值
            if (beforeValue.compareTo(nowPrice) > 0) {
                count++;
            } else {
                if (count >= tickCount) {
                    resultList.add(count);
                }
                count = 0;
            }
            beforeValue = nowPrice;
        }
        if (resultList != null && resultList.size() > 0) {
            return Collections.max(resultList);
        } else {
            return 0;
        }

    }

    /**
     * @param dataList
     * @return
     * @desc 获取平均换手率
     */
    public static BigDecimal getAvgCH(List<DayInfo> dataList) {

        BigDecimal totalPrice = new BigDecimal(0);
        for (DayInfo dayInfo : dataList) {
            totalPrice = totalPrice.add(dayInfo.getChangeHand());
        }
        return totalPrice.divide(new BigDecimal(dataList.size()), 2, RoundingMode.HALF_UP);
    }

    public static void dealBelong() {
        String sql = "select * from stock_info";
        List<StockInfo> result = null;
        try {
            result = JdbcUtil.getDb().query(sql, StockInfo.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        /**
         * 根据股票的代码来判断，
         * 其中上证股票60开头的，
         * 科创板块的个股，其代码688开头，
         * 深证股票00开头的，
         * 中小板股票002开头的，
         * 创业板股票300开头的，
         */
        String updateSql = "";
        int belong = -1;
        for (StockInfo stockInfo : result) {
            final String code = stockInfo.getdCode();
            if (code.startsWith("60")) {
                belong = 1;
            } else if (code.startsWith("688")) {
                belong = 4;
            } else if (code.startsWith("300")) {
                belong = 3;
            } else {
                belong = 2;
            }
            updateSql = "update stock_info set d_belong = " + belong + " where id=" + stockInfo.getId();

            try {
                JdbcUtil.getDb().execute(updateSql, null);
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        dealBelong();
    }
}
